package com.yst.webplugin.api;

import com.yst.web.utils.UrlUtil;
import com.yst.webplugin.annotation.HttpInfo;
import com.yst.webplugin.component.ControllerUtil;
import com.yst.webplugin.pathhandler.IPathHandler;
import org.springframework.context.ApplicationContext;

public class ApiPathHandler implements IPathHandler {

    private String urlPath="api";


    private ApiApplication apiApplication;

    public ApiPathHandler(ApplicationContext context){
        String path= context.getEnvironment().getProperty("api.url","api");
        urlPath=UrlUtil.comboPath(ControllerUtil.getContextPath(context),path);
        apiApplication=new ApiDocBuilder(context).build();
    }

    @Override
    public Boolean supports(String path) {
        return urlPath.equals(UrlUtil.formatUrl(path));
    }

    @Override
    public boolean handle(HttpInfo httpInfo) {
        httpInfo.writeResult(apiApplication);
        return true;
    }

}
